package newcoder;
import java.util.*;
/**
 * @Author 肖地洋
 * @Date 2022/7/15 10:50
 */
public class NC204 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param root TreeNode类
     * @return int整型
     */

    public static void main(String[] args) {
        NC204 nc204 = new NC204();

    }
    public int widthOfBinaryTree (TreeNode root) {
        // write code here
        if(root==null) return 0;
        int res = 1;
        LinkedList<TreeNode> list  = new LinkedList<>();
        list.offer(root);
        while(!list.isEmpty()){
            while (!list.isEmpty()&&list.peekFirst()==null) list.pollFirst();
            while (!list.isEmpty()&&list.peekLast()==null) list.pollLast();
            int len = list.size();
            res = Math.max(res,len);
            for(int i=0;i<len;i++){
                TreeNode node  = list.pollFirst();
                list.addLast(node==null?null:node.left);
                list.addLast(node==null?null:node.right);
            }
        }
        return res;
    }

public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
      this.val = val;
    }
  }



}